#!/bin/bash

if [ $# -ne 1 ];then
	echo "Usage: $0 obs_pro_name"
	echo "Example: $0 Mainline/20.09/Factory"
	exit 1
else
	if [ -f /etc/yum.repos.d/119.3.219.20_82_openEuler_$1_standard_aarch64_.repo ];then
		rm -f /etc/yum.repos.d/119.3.219.20_82_openEuler_$1_standard_aarch64_.repo >/dev/null
	fi
	name=`date +%F-%R`
	printf "###########\t\tcheck openEuler:$1 install rely log\t\t###########\n" >> ${name}_log
	yum-config-manager --add-repo http://119.3.219.20:82/openEuler:/$1/standard_aarch64/ &>/dev/null
	dnf repoclosure --repo 119.3.219.20_82_openEuler_$1_standard_aarch64_ --arch aarch64 --arch noarch &>/dev/null >> ${name}_log
	yum list --repo 119.3.219.20_82_openEuler_$1_standard_aarch64_ | grep ".src" > ${name}_src_pkglist
	#rm -f /etc/yum.repos.d/119.3.219.20_82_openEuler_$1_standard_aarch64_.repo &>/dev/null
	cat ${name}_log | grep -v "unresolved" > nothing_provides_log
	sed -i 's/^[ ]*//g' nothing_provides_log
	sed -i '1,2d' nothing_provides_log
	cat nothing_provides_log | while read line
	do
		if [[ ${line} =~ "package:" ]] &>/dev/null;then
			echo "${line% from*}" >> ${name}_unresolved_src_rpm_log
			continue
		fi
		dnf repoquery --whatprovides "${line}" > res
		wpname=$(cat res | head -1)
		# dnf repoquery --whatprovides return null
		if [ -z ${wpname} ] &>/dev/null;then
			echo "Source       : ${line}" >> ${name}_unresolved_src_rpm_log
		else
			#according binary pkgname to Find source pkgname
			dnf repoquery -i "${wpname%%-[0-9]*}" | grep Source | grep -v "None" | head -1 >> ${name}_unresolved_src_rpm_log
		fi
		rm -f res >/dev/null
	done
	rm -f nothing_provides_log &>/dev/null
	echo "You can see log in file ${name}_log and ${name}_src_pkglist and ${name}_unresolved_src_rpm_log"
	exit 0
fi
#	yum-config-manager --add-repo http://119.3.219.20:82/openEuler:/20.09/standard_aarch64/ >/dev/null
#	dnf repoclosure --repo 119.3.219.20_82_openEuler_20.09_standard_aarch64_ --arch aarch64 --arch noarch >> ${name}
